//
//  HMEmoticonCollectionViewCell.swift
//  Black22
//
//  Created by Apple on 16/11/26.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  自定义表情视图cell
class HMEmoticonCollectionViewCell: UICollectionViewCell {
    
    var indexPath: IndexPath? {
        didSet {
            guard let idp = indexPath else {
                return
            }
            messageLabel.text = "当前显示第\(idp.section + 1)组第\(idp.item + 1)行"
        }
    }
    
    // MARK:    -懒加载
    lazy var emoticonButtonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    
    
    //  表情模型数组
    var emoticonArray: [HMEmoticon]? {
        didSet {
        
            //  遍历数组模型
            guard let etnArray = emoticonArray else {
                return
            }
            
            //  隐藏所有的按钮，在绑定数据的时候显示图片
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            for (i, emoticon) in etnArray.enumerated() {
                let emoticonButton = emoticonButtonArray[i]
                //  给按钮绑定表情模型
                emoticonButton.emoticon = emoticon
                emoticonButton.isHidden = false
                if emoticon.type == "0" {
                    //  图片
                    emoticonButton.setImage(UIImage(named: emoticon.fullPath!), for: .normal)
                    emoticonButton.setTitle(nil, for: .normal)
                } else {
                    //  文字
                    emoticonButton.setTitle((emoticon.code! as NSString).emoji(), for: .normal)
                    emoticonButton.setImage(nil, for: .normal)
                }
                
                
            }
            
            
        }
    }
    
    // MARK:    --懒加载控件
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.brown)
        return label
    }()
    
    //  删除表情按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        addChildButton()
        
        contentView.addSubview(deleteButton)
        
//        addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
    }
    
    //  添加表情子按钮
    fileprivate func addChildButton() {
        for _ in 0..<20 {
            let button = HMEmoticonButton()
            button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 34)
            
            contentView.addSubview(button)
            emoticonButtonArray.append(button)
        }
        
    }
    
    // MARK:    --按钮点击处理逻辑
    @objc private func deleteButtonAction() {
        NotificationCenter.default.post(name: NSNotification.Name(ClickDeleteEmoticonNoticationName), object: nil)
    }
    
    @objc private func btnAction(btn: HMEmoticonButton) {
    
        let emoticon = btn.emoticon!
        //  发送通知
        NotificationCenter.default.post(name: NSNotification.Name(ClickEmoticonButtonNoticationName), object: emoticon)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //  每项的按钮的宽度
        let itemWidth = width / 7
        //  每项的按钮的高度
        let itemheight = height / 3
        
        for (i, button) in emoticonButtonArray.enumerated() {
        
            //  行的索引
            let colIndex = i % 7
            //  列的索引
            let rowIndex = i / 7
            button.size = CGSize(width: itemWidth, height: itemheight)
            button.x = CGFloat(colIndex) * itemWidth
            button.y = CGFloat(rowIndex) * itemheight
            
            
        }
        
        deleteButton.size = CGSize(width: itemWidth, height: itemheight)
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemheight
        
    }
    
    
    
}
